<?php
/*
* Input:  fieldspec, Filters structure (array)
* return:  HTML for filters box 
* 
*
*/

class CF_FilterForm {

	/**
	* @var string $selfpage References the page (usually $_PHP['SELF'])
	*/
	var $selfpage;
	/**
	* @var string $feildspec describes columns and option
	*/	
	var $fieldspec;
	/**
	* @var string $filterprefix prefix for filtered variables (column name used)
	*/
	var $filterprefix = '';	
	/**
	* @var array $filters Holds fitlers as array filers[<column>] = <value> filled by get_filter_vals().  This expects filter variables form the $_REQUEST or from the $_SESSION if saved (pass empty to clear).  Uses prefix determined by $filterprefix
	*/
	var $filters; 

	public static $none_entry = array(
		0=>array('keyvalue' => '', 'showvalue' => '')
		
	);

	public static $yesno_entry = array(
		0=>array('keyvalue' => '', 'showvalue' => ''),
		1=>array('keyvalue' => 0, 'showvalue' => 'No'),
		2=>array('keyvalue' => 1, 'showvalue' => 'Yes')
	);

	
	var $lookup_lists;
	var $index_lookups;
	/**
	* @var bool $multiselect Determines whether to  implement multi-change logic on table
	*/
	/**
	* @var bool $show_filter_submit show sumbit filter (Default true)
	*/   	
    var $show_filter_submit = true;
	/**
	* @var string $show_filter_value string to show on filter button (default 'Apply')
	*/   	
	var $show_filter_value = 'Apply';
	/**
	* @var string $addmarkup string of markup to put in add form for other hidden fields, etc.
	*/  

	// this is for old style fieldspec
//	public static $ctl_mapping = array('T'=>'CtlText','D'=>'CtlSelect','A'=>'CtlTextArea','CAL'=>'CtlJQDate','C'=>'CtlCheckbox');
	
	public static $ctl_mapping = array('ctl_text'=>'CtlText','ctl_select'=>'CtlSelect','ctl_textarea'=>'CtlTextArea','ctl_jqdate'=>'CtlJQDate','C'=>'CtlCheckbox');
	
	
	//$ctl_mapping['T'] = 'CtlText';
	//$ctl_mapping['D'] = 'CtlSelect';
	//$ctl_mapping['A'] = 'CtlTextArea';
	//$ctl_mapping['CAL'] = 'CtlJQDate';
	//$ctl_mapping['C'] = 'CtlCheckbox';
	
	function __construct($fieldspec,$filters,$selfpage = ''){
		$this->fieldspec = $fieldspec;
		$this->filters = $filters;
		if($selfpage == ''){
			$selfpage = $_SERVER['PHP_SELF'];
		}
		$this->selfpage = $selfpage;
	}
	
	
	function set_lookup_lists($ar_lists){
		if(is_array($ar_lists)){
		foreach($ar_lists AS $col=>$lookup_list){
			$this->set_lookup_list($col,$lookup_list);
		}
		}

	}
	
	
	function set_lookup_list($column,$list){
		$this->lookup_lists[$column] = $list;
			//$index = array();
		foreach($list AS $row=>$pair){
			$index[$list[$row]['keyvalue']] = $list[$row]['showvalue'];
		}
		$this->index_lookups[$column] = $index;
	}
	
	function fix_fs_filters(){
		if(is_array($this->fieldspec)){
        foreach($this->fieldspec AS $col_name => $colspec){
			if($colspec['filter'] == 1){
				//$filterparam = 
				$this->fieldspec[$col_name]['filter'] = array( $this->filterprefix . $col_name => array('var'=>$col_name, 'show'=>$colspec['showfilter'] ));
	
			}
		}
		}
	}
	
	/**
	 *
	 * make_filters_box  Method to create a fitlers box which will show each editable
	  * value in it's own text entry or drop-down box for filtering
	  * This function will need the $filters array, which should include a filter ($filters[<filterparam>] = array)
	  * for each filter, even if there is no data in the array.
	 * @param string $method Form Method (get or post)
	 *
	 */
	 
	 
	 
	 
	 
    function make_filters_elements($method = 'get'){
		//print_r($this->index_lookups);
		$this->fix_fs_filters();
		$showbox = false; // default to not showing anything, if there are not filters defined or active
		$filterbox .= "<table class='filterbox'><form name='filters_box' action='$this->selfpage' method='$method'>\n";
        $count = 1;
		if(is_array($this->fieldspec)){
        foreach($this->fieldspec AS $col_name => $colspec){
			if(is_array($colspec['filter'])){
			//print " <pre> [ $col_name ] is a filterarray \n";
			//print_r($colspec['filter']);
			
			//echo 'FILTERS';
			//print_r($this->filters);
			//print "</pre>";
			foreach($colspec['filter'] AS $filterparam=>$filterspec){
				if(!empty($filterspec['show'])){ // insert AND 
					//print "GOING TO SHOW $col_name ";
						$showbox = true;
						if(!empty($filterspec['filterhead'])){
							$head = $filterspec['filterhead'] ;
						}else{
							$head =  $filterparam;
						}
						$filtername = $filterparam;
					   // $elements[$count]['body'] = MakeEditField($this->fieldspec[$col_name], $filtername, $this->filters[$filtername]['val'], 'this.form.submit()', $filtername, $none_entry,$this->top_dir);
					   // $ele_params['id'] = $filterparam;
						// $ele_params['name'] = $filterparam;
						
					$body = $this->make_filter_control($col_name,$colspec,$filterparam);
					$count ++;
					$elements[] = array('head'=>$head, 'body'=>$body);				
				}// end colpsec filter set
			}// end foreach filterspec

		}// end is array
        }// end foreach column in fieldspec
		}
		//print "Elements:\n";
		//print_r($elements);
		if($showbox){
			return $elements;
		}
	}
	
	
	function make_filters_box($method = 'get'){
	
		$filterbox .= "<table class='filterbox'><form name='filters_box' action='$this->selfpage' method='$method'>\n";	
		$elements = $this->make_filters_elements($method);
	
        $filterbox .= $this->make_element_set($elements, "class='filter_head'","class='filter_head'");		
        if($this->show_filter_submit){
            $filterbox .= "<tr><td colspan='$count' > <input type='hidden' name='$this->startatvar' value='0'> <input type='submit' id='refresh' name='refresh' value='$this->show_filter_value'></form> </td></tr>\n";
        }else{
            $filterbox .= "</form>";
         }
         $filterbox .= "</table>";
	 
			return $filterbox;

    }
	
	function make_filter_control($col_name,$colspec,$filterparam){
	    $ele_params['id'] = $filterparam;
		$ele_params['name'] = $filterparam;
		// adjusted this as we get away from using the select
		//$type = self::$ctl_mapping[$colspec['select']];
		$type = self::$ctl_mapping[$colspec['edit_as']];
		//print 'Type is ' . $type . "\n";
		if(isset($this->filters[$filterparam]['val'])){
			$value = $this->filters[$filterparam]['val'];
		}else{
			$value = '';
		}
		
		
		if($type == 'CtlCheckbox'){
			$body = MakePhpCtl('CtlSelect',$ele_params,$value,self::$yesno_entry);
							
		}else{
		
			$body = MakePhpCtl($type,$ele_params,$value,@array_merge(self::$none_entry,$this->lookup_lists[$col_name]));
		}	
		return $body;
	}
	
	
	/**
	 *
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }


}

?>